package com.system.service.monitor.logs.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.constant.HTTPStatus;
import com.system.entity.monitor.logs.SysOperLog;
import com.system.mapper.monitor.logs.SysOperLogMapper;
import com.system.response.Result;
import com.system.response.monitor.logs.OperLogDtoResponse;
import com.system.service.monitor.logs.SysOperLogService;
import com.system.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Classname SysOperLogServiceImpl
 * @Date 2023/8/1 14:24
 * @Created ZFC
 */
@Service
@Slf4j
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    @Autowired
    private SysOperLogMapper operLogMapper;

    @Override
    public void insertOperLog(SysOperLog operLog) {
        operLogMapper.insert(operLog);
    }

    @Override
    public Result deleteOperLogByIds(Long[] ids) {
//        List<Long> stream = Arrays.asList(ids);
//        Collection<Long> operId = new HashSet<>(stream);
//        this.removeByIds(operId);
        for (Long id : Arrays.asList(ids)) {
            operLogMapper.delete(
                    new LambdaQueryWrapper<SysOperLog>()
                            .eq(SysOperLog::getOperId,id)
            );
        }
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("删除成功!");
    }

    @Override
    public Result cleanOperLog() {
        operLogMapper.delete(new LambdaQueryWrapper<>());
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("清空成功!");
    }

    @Override
    public Result selectOperLogList(OperLogDtoResponse operLogDto) {
        if (operLogDto.getPageSize()==null || operLogDto.getPageSize()<0) operLogDto.setPageSize(1);
        if (operLogDto.getPageNum()==null || operLogDto.getPageNum()<10) operLogDto.setPageNum(10);

        PageHelper.startPage(operLogDto.getPageSize(),operLogDto.getPageNum());
        List<SysOperLog> operLogList = operLogMapper.selectList(
                new LambdaQueryWrapper<SysOperLog>()
                        .eq(StringUtils.isNotNull(operLogDto.getBusinessType()), SysOperLog::getBusinessType, operLogDto.getBusinessType())
                        .eq(StringUtils.isNotNull(operLogDto.getStatus()), SysOperLog::getStatus, operLogDto.getStatus())
                        .ge(StringUtils.isNotEmpty(operLogDto.getStartTime()), SysOperLog::getOperTime, operLogDto.getStartTime())
                        .le(StringUtils.isNotEmpty(operLogDto.getEndTime()), SysOperLog::getOperTime, operLogDto.getEndTime())
                        .like(StringUtils.isNotEmpty(operLogDto.getTitle()), SysOperLog::getTitle, operLogDto.getTitle())
                        .like(StringUtils.isNotEmpty(operLogDto.getOperName()), SysOperLog::getOperName, operLogDto.getOperName())
                        .orderByDesc(SysOperLog::getOperTime)
        );
        PageInfo pageInfo = new PageInfo(operLogList);

        Integer count = operLogMapper.selectCount(
                new LambdaQueryWrapper<SysOperLog>()
                        .eq(StringUtils.isNotNull(operLogDto.getBusinessType()), SysOperLog::getBusinessType, operLogDto.getBusinessType())
                        .eq(StringUtils.isNotNull(operLogDto.getStatus()), SysOperLog::getStatus, operLogDto.getStatus())
                        .ge(StringUtils.isNotEmpty(operLogDto.getStartTime()), SysOperLog::getOperTime, operLogDto.getStartTime())
                        .le(StringUtils.isNotEmpty(operLogDto.getEndTime()), SysOperLog::getOperTime, operLogDto.getEndTime())
                        .like(StringUtils.isNotEmpty(operLogDto.getTitle()), SysOperLog::getTitle, operLogDto.getTitle())
                        .like(StringUtils.isNotEmpty(operLogDto.getOperName()), SysOperLog::getOperName, operLogDto.getOperName())
        );
        pageInfo.setSize(count);

        return new Result().setCode(HTTPStatus.SUCCESS).setData(pageInfo);
    }
}
